Jagged Arrays
Jagged arrays are like multidimensional arrays in that you can have multiple dimensions. They differ in that a jagged array will use only the specific amount of memory allocated to it, whereas a multidimensional array uses a uniform amount of memory for every dimension. The jagged array has characteristics of a single dimension array in that it is an array of arrays.

Syntax: type [][]array=new type[size][]

Ex:      int[][] Arr = new int[3][];       
Arr[0] = new int[] {10, 20, 30};
Arr[1] = new int[] {40, 50, 60, 70};
Arr[2] = new int[] {80, 90, 100, 110, 120};
 

 

The rectangular array has a single array object, while the jagged array has four array
objects.

Program on Jagged Arrays to prnt the nos in the following format
1
12
123
1234
12345

 

    class sample
{
static void Main(string[] args)
{
int[][] a = new int[5][];
for (int i = 0; i < 5; i++)
{
a[i] = new int[i + 1];
for (int j = 0; j <= i; j++)
{
a[i][j] = j + 1;
}
}
for (int i = 0; i < 5; i++)
{

for (int j = 0; j <= i; j++)
{
Console.Write(a[i][j]);
}
Console.WriteLine();
}

        }
}

Program on foreach statement with jagged arrays
12345
1234
123
12
1
class sample
{
static void Main(string[] args)
{
int[][] a = new int[5][];
for (int i = 4; i >=0; i--)
{
a[4-i] = new int[i+1];
for (int j = 0; j <= i; j++)
{
a[4-i][j] = j + 1;
}
}

foreach (int[] b in a)
{
foreach (int k in b)
{
Console.Write(k);
}
Console.WriteLine();
}

        }
}